﻿using System.Data;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using YiTu.DBUtility;
using tour.szhome.map.DTO;
using TOUR.SZHOME.Entity;

namespace TOUR.SZHOME.DAL
{
    public partial class Projects_ImageDAL
    {
        /// <summary>
        ///  获取相册列表
        /// </summary>
        /// <param name="XMID"></param>
        /// <returns></returns>
        public static List<AlbumDTO> GetAlbumList(int XMID)
        {
            List<AlbumDTO> list = new List<AlbumDTO>();

            string sql = @"select Projects_Image.XMID,Projects.XMMC, count(0) as Total,Projects.XMLBT
                            from dbo.Projects_Image
                            inner join dbo.Projects on Projects.XMID = Projects_Image.XMID ";
                            if (XMID != 0)
	                        {
		                        sql += " where Projects_Image.XMID = @XMID ";
	                        }
                            sql += " group by Projects_Image.XMID,Projects.XMMC,Projects.XMLBT";
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@XMID", SqlDbType.Int, 0, ParameterDirection.Input, XMID));

            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, sql, commandParms))
            {
                while (reader.Read())
                {
                    list.Add(new AlbumDTO() {
                        XMID = Convert.ToInt32(reader["XMID"]),
                        XMMC = reader["XMMC"].ToString(),
                        Total = Convert.ToInt32(reader["Total"]),
                        XMLBT = reader["XMLBT"].ToString()
                    });
                }
            }
            return list;
        }

        /// <summary>
        /// 根据项目ID获取相册详情列表
        /// </summary>
        /// <param name="XMID"></param>
        /// <returns></returns>
        public static List<Projects_ImageEntity> GetByXMID(int XMID)
        {
            List<Projects_ImageEntity> list = new List<Projects_ImageEntity>() ;
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@XMID", SqlDbType.Int, 0, ParameterDirection.Input, XMID));
            string sql = "select * from dbo.Projects_Image where XMID = @XMID";
            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, sql, commandParms))
            {
                while (reader.Read())
                {
                    list.Add(ConvertToEntityFromDataReader(reader));
                }
            }

            return list;
        }

        /// <summary>
        /// 获取特定项目下某类型图片的相册
        /// </summary>
        /// <param name="XMID"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<Projects_ImageEntity> GetByIdAndType(int XMID, int type)
        {
            List<Projects_ImageEntity> list = new List<Projects_ImageEntity>();
            List<SqlParameter> commandParms = new List<SqlParameter>();
            commandParms.Add(SqlHelper.CreateParam("@XMID", SqlDbType.Int, 0, ParameterDirection.Input, XMID));
            commandParms.Add(SqlHelper.CreateParam("@TPLX", SqlDbType.TinyInt, 0, ParameterDirection.Input, type));
            string sql = "select * from dbo.Projects_Image where XMID = @XMID and TPLX = @TPLX";
            using (SqlDataReader reader = SqlHelper.ExecuteReader(ConnectionString, CommandType.Text, sql, commandParms))
            {
                while (reader.Read())
                {
                    list.Add(ConvertToEntityFromDataReader(reader));
                }
            }

            return list;
        }
    }
}
